// pages/article/articleDetail/articleDetali.js
import getIn from '../../sdk/utils/getIn';
import { API_ARTICLE_CATE, API_ARTICLE_DETAIL } from '../../sdk/business/url.js';
import {showSuccess,showFail} from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      menuItems: [],
      currentIndex: 0,
      loading: false,
      showList: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      const id = getIn(options, ['id']);
      const nature = getIn(options, ['nature']);
      const cate_name = getIn(options, ['cate_name']);
      this.articleCate();
      wx.setNavigationBarTitle({
          title: cate_name
      });
      if (nature == '0') {
          this.articleDetail(id)
      } else {
          this.setData({ showList: true }, () => {
              this.articleList = this.selectComponent("#articleList");
              this.articleList.show(id, cate_name);
          })
      }
  },
    articleCate() {
        const token = wx.getStorageSync('token');
        const id = getIn(this.options, ['id']);
        global.request(API_ARTICLE_CATE, {token}, (code, res) => {
            if (code = 'SUCCESS') {
                const menuItems = getIn(res.data, ['list']);
                const index = menuItems && id && menuItems.findIndex(x => x.id == id);
                const currentIndex = index > -1 ? index : 0;
                this.setData({ menuItems, currentIndex });
                wx.hideLoading()
            } else if(code == 'FAIL') {
                showFail(res.msg)
                wx.hideLoading()
            }
        });
    },
    articleDetail(id) {
      const params = { fetch_key: 'cate_id', cate_id:id};
        this.setData({ loading: true });
        wx.showLoading();
        global.request(API_ARTICLE_DETAIL, params, (code, res) => {
            if (code = 'SUCCESS') {
                const newsDetail = getIn(res.data, ['content']);
                this.setData({ newsDetail, loading: false });
                wx.hideLoading()
            } else if (code == 'FAIL') {
                showFail(res.msg)
                this.setData({ loading: false });
                wx.hideLoading()
            }
        });
    },
    // 点击menu回调函数
    menuCallBackAction(e) {
        const index = getIn(e, ['detail']);
        const { menuItems, currentIndex } = this.data;
        if (currentIndex != index && !this.data.loading) {
            const id = menuItems[index].id;
            wx.setNavigationBarTitle({
                title: menuItems[index].cate_name
            });
            if (!menuItems[index].nature) {
                this.articleDetail(id);
                this.setData({ currentIndex: index, showList: false });
            } else {
                this.setData({ currentIndex: index, showList: true }, () => {
                    this.articleList = this.selectComponent("#articleList");
                    this.articleList.show(id, menuItems[index].cate_name);
                })
            }
        }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})